package com.bishe.bishe.Controller;

import com.bishe.bishe.Service.CustomerService;
import com.bishe.bishe.entity.PageResult;
import com.bishe.bishe.pojo.Customer;
import com.bishe.bishe.pojo.Species;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @RequestMapping("/findall")
    public PageResult<Customer> findall(int page, int size){
        return customerService.findall(page, size);
    }
    @RequestMapping("/findList")/*模糊查询*/
    public List<Customer> findList(@RequestBody Map searchMap){
        return customerService.findList(searchMap);
    }
    @RequestMapping("/findPage")/*分页+模糊查询，和上一个有区别，这个需要map数据*/
    public PageResult<Customer> findPage(@RequestBody Map searchMap,int page,int size){
        return customerService.findPage(searchMap,page,size);
    }
    @RequestMapping("/delete")
    public int delete(int customerId){
        return customerService.delete(customerId);
    }

    @RequestMapping("/update")
    public int update(Customer customer){
        return customerService.update(customer);
    }

    @RequestMapping("/insert")
    public int insert(@RequestBody Customer customer){
        return  customerService.insert(customer);
    }
    @RequestMapping("/find")
    public  int findByNumber(@RequestBody Customer customer){
        int byNumber = customerService.findcustomer(customer);
        if (byNumber==0){
            return  insert(customer);//没有该用户，所以添加，返回添加后的id
        }else {
            return   0;
        }
    }

    @RequestMapping("/select")
    public  Customer select(@RequestBody Customer customer){
        return  customerService.select(customer);
    }

    @RequestMapping("/findall1")
    public List<Customer> findall1(){
        return customerService.findall1();
    }


    @RequestMapping("/findCustomer1")
    public Customer findcustomer(@Validated @RequestParam(name = "customerNumber",required = false) String customerNumber) {
        System.out.println("+++++++++++++++++"+customerNumber);
        return customerService.findCustomer1(customerNumber);
    }
}
